package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.Qiniu;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/travelCommend")
public class TravelCommendController {

    @Autowired
    private ITravelCommendService commendService;

    @RequestMapping("/saveOrUpdate")@ResponseBody
    public Object saveOrUpdate(TravelCommend commend, MultipartFile image) {
        JSONResult json = new JSONResult();
        try {
            if (image != null) {
//                String upload = UploadUtil.upload(image, UploadUtil.IMG_BASE_PATH + "/upload");
                String upload = Qiniu.upLoadImage(image.getBytes());
                json.setData(upload);
                commend.setCoverUrl(upload);
            }
            commendService.saveOrUpdate(commend);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") TravelCommendQueryObject qo, Model model) {
        qo.setOrderBy("tc.schedule desc");
        PageInfo result = commendService.list(qo);
        model.addAttribute("result", result);
        return "/travel/commendList";
    }

    @RequestMapping("/selectForCommend")@ResponseBody
    public Object selectForCommend(Long travelId){
        JSONResult json = new JSONResult();
        try {
            commendService.selectByTravelId(travelId);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
}


